package win.zxshuo.www.controller.apis;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import win.zxshuo.www.controller.responses.BaseResponse;
import win.zxshuo.www.controller.responses.BooksResponse;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.model.chapters.ChapterModel;
import win.zxshuo.www.service.BookService;
import win.zxshuo.www.service.ChapterService;
import win.zxshuo.www.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/book")
public class BookApiController {
    Log logger = LogFactory.getLog(getClass());

    @Autowired
    BookService bookService;

    @Autowired
    ChapterService chapterService;

    /**
     * 添加书籍接口
     * @param name
     * @param surface
     * @param category
     * @param author
     * @param introduction
     * @param status
     * @param mark
     * @param qidianurl
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse addBook(String name,String surface,String category,String author,String introduction,Integer status,Double mark,String qidianurl){
        //TODO 需要做验证器来做验证，否则可能会出问题
        BaseResponse baseResponse = new BaseResponse();
        try {
            bookService.addNewBook(name,surface,category,author,introduction,status,mark,qidianurl);
            baseResponse.setTrue();
        }catch (Exception e){
            baseResponse.setErrorMsg(e.getMessage());
        }
        baseResponse.setTrue();
        return baseResponse;
    }

    /**
     * 通过书籍id来获取书籍信息
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public BaseResponse getBookInfo(@PathVariable Long id){
        BaseResponse baseResponse = new BaseResponse();
        try {
            BookModel bookModel = bookService.getBookById(id);
            BooksResponse booksResponse = new BooksResponse(bookModel,chapterService.getFirstChapter(bookModel),chapterService.getLastChapter(bookModel));
            return booksResponse;
            //TODO 如果是已登录用户，则查看是否已经加入书架
        }catch (Exception e){
            logger.error(e.getCause());
            baseResponse.setErrorMsg(e.getMessage());
        }
        return baseResponse;
    }

    /**
     * 通过书籍id字符串来获取书籍id 字符样例为： 1,2,3,4, 如此样例
     * @param ids
     * @return
     */
    @RequestMapping("/getall/{ids}")
    public BaseResponse getInfoOfBooks(@PathVariable String ids){
        System.out.println("ids = [" + ids + "]");
        BaseResponse baseResponse = new BaseResponse();
        try{
            List<Long> intIds = StringUtils.parseIdsToIntIds(ids);
            List<BookModel> bookModels = bookService.getBooksByIds(ids);
            BooksResponse booksResponse = new BooksResponse(bookModels,chapterService);
            return booksResponse;
        }catch (Exception e){
            logger.error(e.getCause());
            baseResponse.setErrorMsg(e.getMessage());
        }
        baseResponse.setData("");
        return baseResponse;
    }

    @GetMapping("/getindex/{bookid}")
    public BaseResponse getBookIndex(@PathVariable("bookid") Long bookid){
        BaseResponse baseResponse = new BaseResponse();
        try{
            BookModel bookModel = bookService.getBookById(bookid);
            List<ChapterModel> chapterModels = chapterService.getAllChaptersBy(bookModel);
            List<Map<String,Object>> datas = chapterService.translateBookModelToListMap(chapterModels);
            baseResponse.setTrueData(datas);
        }catch (Exception e){
            baseResponse.setErrorMsg(e.getMessage());
        }
        return baseResponse;
    }

//    /**
//     * 获取书籍更新信息
//     * @param ids
//     * @return
//     */
//    @RequestMapping("/{ids}/getupdate")
//    public BaseResponse getBookUpdates(@PathVariable String ids){
//        BaseResponse baseResponse = new BaseResponse();
//
//        return b
}
